package com.wy.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wy.model.Result;
import com.wy.model.Room;
import com.wy.service.RoomService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.UUID;

/**
 * @描述
 * @创建人 wangyue
 * @创建时间2019/12/216:25
 */
@Api("restful风格的api示例")
@RestController
@RequestMapping("room")
public class RoomController {

    @Autowired
    private RoomService roomService;

    @ApiOperation(value = "查询列表")
    @GetMapping("list")
    public Result<Room> list1(HttpServletRequest request, Room room,
                              @RequestParam(defaultValue = "1") int pagenum,
                              @RequestParam(defaultValue = "2") int pagesize){
        PageHelper.startPage(pagenum,pagesize);
        List<Room> roomList = roomService.list(room);
        PageInfo<Room> pageInfo = new PageInfo<>(roomList);
        return new Result(pageInfo.getTotal(),pageInfo.getList());
    }


    @ApiOperation(value = "查询单条")
    @ApiImplicitParams({@ApiImplicitParam(name = "id",value = "用户id",required = true,paramType = "path")})
    @ApiResponses({@ApiResponse(code=400,message="id为空")})
    @GetMapping("getById/{id}")
    public Room getById(@PathVariable String id){
        return roomService.getById(id);
    }


    @ApiOperation(value = "新增修改")
    @PostMapping("save")
    public Boolean save(@RequestBody Room room){
        String id = room.getId();
        if(id !=null && !id.equals("")){
            roomService.update(room);
        } else{
            room.setId(UUID.randomUUID().toString());
            roomService.insert(room);
        }
        return true;
    }
    @ApiOperation(value = "删除")
    @DeleteMapping("delete")
    public boolean delete(String ids){
        if(ids != null && !ids.equals("")){
            roomService.deletebth(ids.split(","));
        }
        return true;
    }
}
